package com.yunzhi.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunzhi.base.exception.yunzhiPlusException;
import com.yunzhi.content.mapper.TeachplanMapper;
import com.yunzhi.content.mapper.TeachplanMediaMapper;
import com.yunzhi.content.model.dto.BindTeachplanMediaDto;
import com.yunzhi.content.model.dto.SaveTeachplanDto;
import com.yunzhi.content.model.dto.TeachplanDto;
import com.yunzhi.content.model.po.Teachplan;
import com.yunzhi.content.model.po.TeachplanMedia;
import com.yunzhi.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description 课程计划服务
 */
@Service
public class TeachplanServiceImpl implements TeachplanService {

    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;

    /**
     * 根据课程id查询课程计划
     * @param courseId 课程计划
     * @return
     */
    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        List<TeachplanDto> teachplanDtos = teachplanMapper.selectTreeNodes(courseId);
        return teachplanDtos;
    }

    private int getTeachplanCount(Long courseId,Long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return  count+1;
    }
    @Override
    public void saveTeachplan(SaveTeachplanDto saveTeachplanDto) {
        //通过课程计划id判断是新增和修改
        Long teachplanId = saveTeachplanDto.getId();
        if(teachplanId ==null){
            //新增
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            //确定排序字段，找到它的同级节点个数，排序字段就是个数加1  select count(1) from teachplan where course_id=117 and parentid=268
            Long parentid = saveTeachplanDto.getParentid();
            Long courseId = saveTeachplanDto.getCourseId();
            int teachplanCount = getTeachplanCount(courseId, parentid);
            teachplan.setOrderby(teachplanCount);
            teachplanMapper.insert(teachplan);

        }else{
            //修改
            Teachplan teachplan = teachplanMapper.selectById(teachplanId);
            //将参数复制到teachplan
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }

    }

    @Transactional
    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //课程计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan == null){
            yunzhiPlusException.cast("课程计划不存在");
        }

        //先删除原有记录,根据课程计划id删除它所绑定的媒资
        int delete = teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, bindTeachplanMediaDto.getTeachplanId()));

        //再添加新记录
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto,teachplanMedia);
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMediaMapper.insert(teachplanMedia);

    }
    /**
     * @description 删除课程计划
     */
    @Override
    public void deleteTeachplan(Long teachplanId) {
        //获得课程计划
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        //判断这个课程计划是第几级
        if(teachplan.getGrade()==2) { //如果是第2级那就直接删除课程计划和关联的媒资
            //删除课程计划
            teachplanMapper.deleteById(teachplanId);
            LambdaQueryWrapper<TeachplanMedia> Wrapper = new LambdaQueryWrapper<>();
            //删除与课程计划绑定的媒资
            teachplanMediaMapper.delete(Wrapper.eq(TeachplanMedia::getTeachplanId,teachplanId));
        }
        //如果是第一级,那要判断有没有第二级,没有才能删
        List<Teachplan> teachplanList = teachplanMapper.selectList(new LambdaQueryWrapper<Teachplan>().eq(Teachplan::getParentid, teachplanId));
        if(teachplanList.size()!=0) { //不等于0就代表还有子计划
            yunzhiPlusException.cast("课程计划信息还有子级信息，无法操作");
            return;
        }
        //在这里表示没有子计划,删除课程计划
        teachplanMapper.deleteById(teachplanId);
    }

    /**
     * 向下移動
     * @param teachplanId 课程计划ID
     */
    @Override
    public void movedownTeachplanId(Long teachplanId) {
        //查找出课程计划
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        //找到当前课程计划的下一排序
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        //首先得是同一级
        queryWrapper.eq(Teachplan::getGrade,teachplan.getGrade());
        //其次是我下面的一个
        queryWrapper.eq(Teachplan::getOrderby,teachplan.getOrderby()+1);
        //更要是同一个父ID
        queryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        //找到下一排序
        Teachplan teachplanOne = teachplanMapper.selectOne(queryWrapper);
        //判断是否是最后一个排序
        if(teachplanOne==null) {
            yunzhiPlusException.cast("当前是最后一个列表");
            return;
        }
        //交换位置
        teachplanOne.setOrderby(teachplanOne.getOrderby()-1);
        teachplan.setOrderby(teachplan.getOrderby()+1);
        teachplanMapper.updateById(teachplanOne);
        teachplanMapper.updateById(teachplan);
    }
}
